var form = layui.form,
	layer = layui.layer;

var operation = getQueryString('operation');

console.log("当前执行操作",operation)

if('add'==operation){
	initNetRegion();
}

$("#saveQuartz").click(function(){
	var formData = form.val('server-form');
	
	var key_id = $('#key_id').val();
	
	var server_name = formData.server_name;
	var server_ip = formData.server_ip;
	var server_port = formData.server_port;
	var net_region = formData.net_region;
	var connect_out_time = formData.connect_out_time;
	
	var requestUrl = '/serverlisten/updateserverparam';
	
	if('add'==operation){
		requestUrl = '/serverlisten/addlistenserver';
	}
	
	layer.load(2);
	
	$.ajax({
		type : 'post'
		,dataType : 'json'
		,url : access_url+requestUrl
		,data : {
			key_id : key_id
			,server_name : server_name
			,server_ip : server_ip
			,server_port : server_port
			,net_region : net_region
			,connect_out_time : connect_out_time
		}
		,success : function(data){
			layer.closeAll('loading');
			if(data.code != 200){
				layer.msg(data.msg, {icon: 5});
				return;
			}
			parent.layer.alert(data.msg,{icon:6});
			parent.reloadTable();
			parent.layer.closeAll('iframe');
		}
	})
})

//初始化网络域下拉框的值
function initNetRegion(){
	$.ajax({
		type : 'get'
		,dataType : 'json'
		,async : false
		,url : access_url+'/cache/loadcachedata/getdictsbypcode?parent_code=3000'
		,success : function(data){
			console.log("data",data);
			if(data.code != 200){
				layer.msg(data.msg, {icon: 5});
				return;
			}
			
			var dictlist = data.retlist;
			
			var dictLen = dictlist.length;
			
			var roleLimitHtml = '';
			
			for (var i = 0; i < dictLen; i++) {
				var dict = dictlist[i];
				var htmlModel = '<option value="{word_value}">{word_mean}</option>'
				var opHtml = htmlModel.format(dict);
				roleLimitHtml += opHtml;
			}
			
			$('#net_region').append(roleLimitHtml);
			
			form.val('server-form');
		}
	})
}

/**
 * 服务属性回显
 * @returns
 */
function transmitParam(serverProperties){
	initNetRegion();
	
	var key_id = serverProperties.key_id;
	var server_name = serverProperties.server_name;
	var server_ip = serverProperties.server_ip;
	var server_port = serverProperties.server_port;
	var connect_out_time = serverProperties.connect_out_time;
	var net_region_code = serverProperties.net_region_code;
	
	$('#key_id').val(key_id);
	
	form.val('server-form',{
		'server_name':server_name
		,'server_ip':server_ip
		,'server_port':server_port
		,'connect_out_time':connect_out_time
		,'net_region':net_region_code
	})
}